import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/layout'
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)
/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, 项目将不会显示在侧边栏中(default is false)
 * alwaysShow: true               if set true, 将始终显示根菜单
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, 不会缓存该页(default is false)
    affix: true                  if set true, 标签将粘贴在标签视图中
    breadcrumb: false            if set false,该项目将隐藏在面包屑(default is true)
    activeMenu: '/example/list'  if set path, 侧边栏将突出显示您设置的路径
  }
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        name: 'redirect',
        component: () => import('@/views/redirect/index'),
        menu: 'redirect'
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: 'home',
        component: () => import('@/views/home/index'),
        name: 'home',
        meta: { title: '首页', icon: 'dashboard', affix: true },
        menu: 'home'
      }
    ]
  },
]
export const asyncRoutes = [
  {
    path: '/user',
    component: Layout,
    redirect: '/user/index',
    meta: { title: '用户', icon: 'user',roles:["admin"] },
    children: [
      {
        path: 'index',
        component: () => import('@/views/user/user'),
        name: 'userManage',
        meta: { title: '用户',roles:["admin"] },
        menu: 'userManage'
      },
      {
        path: 'role',
        component: () => import('@/views/user/role'),
        name: 'roleManage',
        meta: { title: '角色',roles:[""] },
        menu: 'roleManage'
      },
      {
        path: 'function',
        component: () => import('@/views/user/function'),
        name: 'functionManage',
        meta: { title: '权限',roles:[""] },
        menu: 'functionManage'
      }
    ]
  },
  {
    path: '/team',
    component: Layout,
    redirect: '/team/index',
    meta: { title: '团队', icon: 'peoples',roles:["teamLeader","teamManager"] },
    children: [
      {
        path: 'team',
        component: () => import('@/views/team/team'),
        name: 'teamManage',
        meta: { title: '团队' },
        menu: 'teamManage'
      },
      {
        path: 'tmember',
        component: () => import('@/views/team/member'),
        name: 'ttypeManage',
        meta: { title: '成员', roles:["teamLeader","teamManager"]},
        menu: 'ttypeManage',
      },
      {
        path: 'ttype',
        component: () => import('@/views/team/type'),
        name: 'ttypeUserManage',
        meta: { title: '类型' },
        menu: 'ttypeUserManage',
      },
    ]
  },
  {
    path: '/bill',
    component: Layout,
    redirect: '/team/bill',
    meta: { title: '账单', icon: 'money' },
    children: [
      {
        path: 'bill',
        component: () => import('@/views/bill/bill'),
        name: 'billManage',
        meta: { title: '账单' },
        menu: 'billManage',
      },
      {
        path: 'btype',
        component: () => import('@/views/bill/type'),
        name: 'btypeManage',
        meta: { title: '类型' },
        menu: 'btypeManage',
      },
    ]
  },
  // {
  //   path: '/detail',
  //   component: Layout,
  //   redirect: '/detail/index',
  //   meta: {roles:["teamLeader","user"] },
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/detail/index'),
  //       name: 'detail',
  //       meta: { title: '明细', icon: 'money',roles:["teamLeader","user"] },
  //       menu: 'detail',
  //     }
  //   ]
  // },
  {
    path: '/detailtest',
    component: Layout,
    redirect: '/detail/index2',
    meta: {roles:["teamLeader","user","teamMember","teamManager"] },
    children: [
      {
        path: 'index',
        component: () => import('@/views/detail/index2'),
        name: 'detailtest',
        meta: { title: '明细', icon: 'money',roles:["teamLeader","user","teamMember","teamManager"] },
        menu: 'detailtest',
      }
    ]
  },
  {
    path: '/member',
    component: Layout,
    redirect: '/member/index',
    meta: {roles:["teamLeader","user","teamManager"] },
    children: [
      {
        path: 'index',
        component: () => import('@/views/member/index'),
        name: 'member',
        meta: { title: '图表', icon: 'excel',roles:["teamLeader","user","teamMember","teamManager"]  },
        menu: 'member'
      }
    ]
  },
  {
    path: '/message',
    component: Layout,
    redirect: '/message/index',
    meta: {roles:["teamLeader","user","teamMember","teamManager"] },
    children: [
      {
        path: 'index',
        component: () => import('@/views/message/index'),
        name: 'admin',
        meta: { title: '消息', icon: 'message',roles:["teamLeader","user","teamMember","teamManager"]},
        menu: 'admin',
      },
    ]
  },
  {
    path: '/admin',
    component: Layout,
    redirect: '/admin/index',
    meta: {roles:["teamLeader","user","teamMember","teamManager"] },
    children: [
      {
        path: 'index',
        component: () => import('@/views/admin/index'),
        name: 'admin',
        meta: { title: '我', icon: 'user',roles:["teamLeader","user","teamMember","teamManager"]},
        menu: 'admin',
      },
    ]
  },
]
const createRouter = () => new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes: constantRoutes
})
const router = createRouter()
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

//获取原型对象上的push函数
export default router
